#include "MessageBox.h"

// set data to defaults
Message_Box::Message_Box() 
{
	m_message = 0;
	m_length = 0;
}

// make sure safer release is called
Message_Box::~Message_Box() { release(); }

// safetly release all data
void Message_Box::release()
{
	if(m_message)
	{
		delete [] m_message;
		m_message = 0;
		m_length = 0;
	}
}

// setup message box
void Message_Box::initialize(char * message, int length, V2DF pos, int width, int height, Orientation drawPoint, Orientation textPoint, int A, int R, int G, int B)
{
	// if a message already exists release it first
	if(m_message)
		release();
	// setup message
	m_length = length;
	m_message = new char[m_length+1];
	for(int i = 0; i < m_length; i++)
	{
		m_message[i] = message[i];
	}
	m_message[m_length] = 0;
	// save position and bounds
	m_pos = pos;
	m_height = height;
	m_width = width;
	// save orientations
	m_drawPoint = drawPoint;
	m_textPoint = textPoint;
	// set color
	m_A = A;
	m_R = R;
	m_G = G;
	m_B = B;
}

// draw the message
void Message_Box::drawText()
{
	// get engine reference
	DX2DEngine * p2dEngine = DX2DEngine::getInstance();

	// build rect from position and orientation
	RECT bounds;
	switch (m_drawPoint)
	{
	case TOP_LEFT:
		bounds.top = -m_height;
		bounds.left = -m_width;
		bounds.right = 0;
		bounds.bottom = 0;
		break;
	case TOP_RIGHT:
		bounds.top = -m_height;
		bounds.left = 0;
		bounds.right = m_width;
		bounds.bottom = 0;
		break;
	case CENTER:
		bounds.top = -m_height/2;
		bounds.left = -m_width/2;
		bounds.right = m_width/2;
		bounds.bottom = m_height/2;
		break;
	case BOT_RIGHT:
		bounds.top = 0;
		bounds.left = 0;
		bounds.right = m_width;
		bounds.bottom = m_height;
		break;
	case BOT_LEFT:
		bounds.top = 0;
		bounds.left = -m_width;
		bounds.right = 0;
		bounds.bottom = m_height;
		break;
	case TOP_CENTER:
		bounds.top = -m_height;
		bounds.left = -m_width/2;
		bounds.right = m_width/2;
		bounds.bottom = 0;
		break;
	case BOT_CENTER:
		bounds.top = 0;
		bounds.left = -m_width/2;
		bounds.right = m_width/2;
		bounds.bottom = m_height;
		break;
	default:
		break;
	}
	// add position
	bounds.right += m_pos.x;
	bounds.left += m_pos.x;
	bounds.top += m_pos.y;
	bounds.bottom += m_pos.y;

	DWORD formating;
	// determine orientation
	switch (m_textPoint)
	{
	case TOP_LEFT:
		formating = DT_TOP | DT_LEFT | DT_NOCLIP;
		break;
	case TOP_RIGHT:
		formating = DT_TOP | DT_RIGHT | DT_NOCLIP;
		break;
	case CENTER:
		formating = DT_CENTER | DT_VCENTER | DT_NOCLIP;
		break;
	case BOT_RIGHT:
		formating = DT_BOTTOM | DT_RIGHT | DT_NOCLIP;
		break;
	case BOT_LEFT:
		formating = DT_BOTTOM | DT_LEFT | DT_NOCLIP;
		break;
	case TOP_CENTER:
		formating = DT_TOP | DT_CENTER | DT_NOCLIP;
		break;
	case BOT_CENTER:
		formating = DT_BOTTOM | DT_CENTER | DT_NOCLIP;
		break;
	default:
		break;
	}
	// write text to the screen
	p2dEngine->writeText(m_message,bounds,D3DCOLOR_ARGB(255,m_R,m_G,m_B),formating);
}